// pages/heatMoney/index.js
const Page = require('../../../utils/ald-stat.js').Page;
const app = getApp();
let interstitialAd = null;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    haveMore:'',
    day: '',
    page: 1,
    total_currency: '',
    today_currency: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    if (wx.createInterstitialAd) {
      interstitialAd = wx.createInterstitialAd({
        adUnitId: 'adunit-06e96f4b0cb5c68f'
      })
      interstitialAd.onError((err) => {
        console.log('onError event emit', err)
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
      })
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this;
    var object = {
      unionid: wx.getStorageSync('openid'),
      page: 1,
    }
    var signs = app.sort_ASCII(object)
    wx.request({
      url: app.globalData.new_url + '/userinfo/Userinfo/currency_list',
      data: {
        page: 1,
        unionid: wx.getStorageSync('openid'),
        sign:signs
      },
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      success: function(res) {
        console.log(res)
        if(res.data.code===200){
          that.setData({
            total_currency: res.data.total_currency,
            today_currency: res.data.today_currency,
            day: res.data.res,
          })
          that.data.haveMore = res.data.more;
        }
      }
    })
  },
  onUnload: function () {
    let pages = getCurrentPages();
    let prevPage = pages[0]
    if (prevPage.route == "pages/tarbar/my/index") {
      prevPage.setData({
        isShowInterstitialAd: 100,
      })
    }
  },

  // 上拉触底事件，请求记录数据
  onReachBottom: function() {
    const that = this
    let page = that.data.page;
    if (that.data.haveMore) {
      // 请求下一页数据
      page++;
      that.data.page = page;
      var object = {
        unionid: wx.getStorageSync('openid'),
        page: page,
      }
      var signs = app.sort_ASCII(object)
      wx.request({
        url: app.globalData.new_url + '/userinfo/Userinfo/currency_list',
        data: {
          page: page,
          unionid: wx.getStorageSync('openid'),
          sign:signs
        },
        method: 'POST',
        header: {
          'content-type': 'application/json'
        },
        success: function(res) {
          console.log(res)
          var day = that.data.day.concat(res.data.res);
          that.data.haveMore = res.data.more;
          that.setData({
            day: day,
          })
        }
      })
    } else {
      console.log('没有更多商品')
    }

  },
  onReady:function(){
    if (interstitialAd) {
      interstitialAd.show().catch((err) => {
        console.error(err)
      })
    }
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    var openid = wx.getStorageSync('openid')
    return {
      title: '步数换好礼，我正在用步数免费领礼品，你也快来！',
      imageUrl: '../../../imgs/share.png',
      path: '/pages/tarbar/index/index?openid=' + openid + '&&jx=55'
    }
  },
})